﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *  
 **/

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using Peter.Common;
using Peter.Common.BindingConverters;
using Peter.Common.Icons;
using Peter.Common.Interfaces;
using Peter.Common.MainMenu;

namespace Peter.Logic
{
   /// <summary>
   /// Manages the application context menus.
   /// </summary>
   public class ModelContextMenu : ViewModelBase
   {
      private ContextMenu m_DocumentContextMenu;

      /// <summary>
      /// Initializes a new context menu model.
      /// TODO: This needs to be done in a more MVVM way...
      /// </summary>
      /// <param name="findFileCommand"></param>
      public ModelContextMenu (ICommand findFileCommand)
      {
         var editableConverter = new TypeVisibilityConverter <IEditable> ();

         var contextMenu = new ContextMenu ();

         var menuItem = new MenuItem { Header = "Save", Icon = new IconDisplay { Icon = MainMenuIcon.SaveFloppy } };
         menuItem.SetBinding (MenuItem.CommandProperty, new Binding ("Model.EditCommand"));
         menuItem.CommandParameter = EditAction.Save;
         menuItem.SetBinding (UIElement.VisibilityProperty, new Binding ("Model") { Converter = editableConverter});
         contextMenu.Items.Add (menuItem);

         var separator = new Separator ();
         separator.SetBinding (UIElement.VisibilityProperty, new Binding ("Model") { Converter = editableConverter });
         contextMenu.Items.Add (separator);

         menuItem = new MenuItem
                       {
                          Header = Xceed.Wpf.AvalonDock.Properties.Resources.Document_Close,
                          Icon = new IconDisplay { Icon = MainMenuIcon.Remove }
                       };
         menuItem.SetBinding (MenuItem.CommandProperty, new Binding ("CloseCommand"));
         contextMenu.Items.Add (menuItem);

         menuItem = new MenuItem
                       {
                          Header = Xceed.Wpf.AvalonDock.Properties.Resources.Document_CloseAllButThis,
                          Icon = new IconDisplay { Icon = MainMenuIcon.RemoveCircle }
                       };
         menuItem.SetBinding (MenuItem.CommandProperty, new Binding ("CloseAllButThisCommand"));
         contextMenu.Items.Add (menuItem);

         contextMenu.Items.Add (new Separator ());

         menuItem = new MenuItem
                       {
                          Header = Xceed.Wpf.AvalonDock.Properties.Resources.Document_Float,
                          Icon = new IconDisplay { Icon = MainMenuIcon.Newwindow }
                       };
         menuItem.SetBinding (MenuItem.CommandProperty, new Binding ("FloatCommand"));
         contextMenu.Items.Add (menuItem);

         menuItem = new MenuItem
                       {
                          Header = Xceed.Wpf.AvalonDock.Properties.Resources.Document_DockAsDocument,
                          Icon = new IconDisplay { Icon = MainMenuIcon.Newtab }
                       };
         menuItem.SetBinding (MenuItem.CommandProperty, new Binding ("DockAsDocumentCommand"));
         contextMenu.Items.Add (menuItem);

         contextMenu.Items.Add (new Separator ());

         menuItem = new MenuItem
                       {
                          Header = Xceed.Wpf.AvalonDock.Properties.Resources.Document_NewHorizontalTabGroup,
                          Icon = new IconDisplay { Icon = MainMenuIcon.Horizontalborder }
                       };
         menuItem.SetBinding (MenuItem.CommandProperty, new Binding ("NewHorizontalTabGroupCommand"));
         contextMenu.Items.Add (menuItem);

         menuItem = new MenuItem
                       {
                          Header = Xceed.Wpf.AvalonDock.Properties.Resources.Document_NewVerticalTabGroup,
                          Icon = new IconDisplay { Icon = MainMenuIcon.Verticalborder }
                       };
         menuItem.SetBinding (MenuItem.CommandProperty, new Binding ("NewVerticalTabGroupCommand"));
         contextMenu.Items.Add (menuItem);

         var documentConverter = new TypeVisibilityConverter<IPeterFileDocument> ();
         separator = new Separator ();
         separator.SetBinding (UIElement.VisibilityProperty, new Binding ("Model") { Converter = documentConverter });
         contextMenu.Items.Add (separator);

         menuItem = new MenuItem { Header = "Copy Full Path" };
         menuItem.SetBinding (MenuItem.CommandProperty, new Binding ("Model.CopyPathCommand"));
         menuItem.CommandParameter = "Full";
         menuItem.SetBinding (UIElement.VisibilityProperty, new Binding ("Model") { Converter = documentConverter });
         contextMenu.Items.Add (menuItem);

         menuItem = new MenuItem { Header = "Copy Directory Path" };
         menuItem.SetBinding (MenuItem.CommandProperty, new Binding ("Model.CopyPathCommand"));
         menuItem.CommandParameter = "Directory";
         menuItem.SetBinding (UIElement.VisibilityProperty, new Binding ("Model") { Converter = documentConverter });
         contextMenu.Items.Add (menuItem);

         menuItem = new MenuItem { Header = "Copy File Name" };
         menuItem.SetBinding (MenuItem.CommandProperty, new Binding ("Model.CopyPathCommand"));
         menuItem.CommandParameter = "Name";
         menuItem.SetBinding (UIElement.VisibilityProperty, new Binding ("Model") { Converter = documentConverter });
         contextMenu.Items.Add (menuItem);

         menuItem = new MenuItem
                       {
                          Header = "Show in Windows Explorer",
                          Icon = new IconDisplay { Icon = MainMenuIcon.Explorerwindow }
                       };
         menuItem.SetBinding (MenuItem.CommandProperty, new Binding ("Model.OpenFolderCommand"));
         menuItem.SetBinding (UIElement.VisibilityProperty, new Binding ("Model") { Converter = documentConverter });
         contextMenu.Items.Add (menuItem);

         menuItem = new MenuItem
         {
            Header = "Find in File Explorer",
            Icon = new IconDisplay { Icon = MainMenuIcon.Foldertree },
            Command = findFileCommand
         };
         menuItem.SetBinding (MenuItem.CommandParameterProperty, new Binding ("Model.FileInfo.FullName"));
         menuItem.SetBinding (UIElement.VisibilityProperty, new Binding ("Model") { Converter = documentConverter });
         contextMenu.Items.Add (menuItem);

         this.DocumentContextMenu = contextMenu;
      }

      /// <summary>
      /// Gets or Sets the document context menu.
      /// </summary>
      public ContextMenu DocumentContextMenu
      {
         get { return this.m_DocumentContextMenu; }
         set
         {
            this.m_DocumentContextMenu = value;
            this.OnPropertyChanged ("DocumentContextMenu");
         }
      }
   }
}
